package com.oig.sys.workflow.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.workflow.dao.ITbDataModuleDetailDao;
import com.oig.sys.workflow.dto.DataModuleDetailDto;
import com.oig.sys.workflow.entity.TbDataModuleDetail;
import com.oig.sys.workflow.service.ITbDataModuleDetailService;
import com.oig.sys.workflow.vo.DataModuleDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbDataModuleDetailServiceImpl implements ITbDataModuleDetailService {

	private final ITbDataModuleDetailDao tbDataModuleDetailDao;

	@Autowired
	public TbDataModuleDetailServiceImpl(ITbDataModuleDetailDao tbDataModuleDetailDao){
		 this.tbDataModuleDetailDao = tbDataModuleDetailDao;
	}


	private DataModuleDetailVo addDataModuleDetail(DataModuleDetailDto dataModuleDetailDto){
		TbDataModuleDetail detail = tbDataModuleDetailDao.getByDataModuleIdAndColumnField(dataModuleDetailDto.getDataModuleId(),dataModuleDetailDto.getColumnField()) ;
		if (detail!=null){
			throw new BadRequestException("field重复") ;
		}
		detail = new TbDataModuleDetail();
		BeanUtils.copyProperties(dataModuleDetailDto, detail, "id");
		this.tbDataModuleDetailDao.save(detail);
		return this.buildVo(detail);
	}

	private DataModuleDetailVo buildVo(TbDataModuleDetail dataModuleDetail){
		DataModuleDetailVo dataModuleDetailVo = new DataModuleDetailVo();
		BeanUtils.copyProperties(dataModuleDetail, dataModuleDetailVo);
		return dataModuleDetailVo;
	}

	private DataModuleDetailVo updateDataModuleDetail(DataModuleDetailDto dataModuleDetailDto){
		TbDataModuleDetail detail = tbDataModuleDetailDao.getDMDById(dataModuleDetailDto.getId());
		if (detail==null){
			throw new BadRequestException("记录不存在") ;
		}
		if (!detail.getDataModuleId().equals(dataModuleDetailDto.getDataModuleId())){
			throw new BadRequestException("明细id传错");
		}
		if (!detail.getColumnField().equals(dataModuleDetailDto.getColumnField())){
			TbDataModuleDetail od = this.tbDataModuleDetailDao.getByDataModuleIdAndColumnField(detail.getDataModuleId(),detail.getColumnField());
			if (od!=null){
				throw new BadRequestException("field重复") ;
			}
		}
		BeanUtils.copyProperties(dataModuleDetailDto, detail);
		this.tbDataModuleDetailDao.save(detail);
		return this.buildVo(detail);
	}

	@Transactional
	@Override
	public List<DataModuleDetailVo> addDataModuleDetailList(Integer moduleId,List<DataModuleDetailDto> dataModuleDetailDtoList) {
		List<DataModuleDetailVo> detailVoList = new ArrayList<>();
		for (DataModuleDetailDto detailDto : dataModuleDetailDtoList){
			detailDto.setDataModuleId(moduleId);
			detailVoList.add(this.addDataModuleDetail(detailDto));
		}
		return detailVoList;
	}

	@Transactional
	@Override
	public List<DataModuleDetailVo> updateDataModuleDetailList(Integer moduleId,List<DataModuleDetailDto> dataModuleDetailDtoList) {
		List<DataModuleDetailVo> detailVoList = new ArrayList<>();
		List<Integer> idList = new ArrayList<>();
		for (DataModuleDetailDto detailDto : dataModuleDetailDtoList){
			detailDto.setDataModuleId(moduleId);
			DataModuleDetailVo detailVo ;
			if (detailDto.getId()!=null){
				detailVo = this.updateDataModuleDetail(detailDto);
			} else {
				detailVo = this.addDataModuleDetail(detailDto);
			}
			idList.add(detailVo.getId());
			detailVoList.add(detailVo);
		}
		List<Integer> delIdList = this.tbDataModuleDetailDao.findAllDelId(moduleId, idList) ;
		if (CollectionUtil.isNotEmpty(delIdList)){
			this.tbDataModuleDetailDao.deleteByIdList(delIdList);
		}
		return detailVoList;
	}



	@Override
	public List<DataModuleDetailVo> findAllByModuleId(Integer moduleId) {
		List<TbDataModuleDetail> dataModuleDetailList = this.tbDataModuleDetailDao.findAllByDataModuleId(moduleId);
		List<DataModuleDetailVo> voList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(dataModuleDetailList)){
			dataModuleDetailList.forEach((item)->voList.add(this.buildVo(item)));
		}
		return voList;
	}
}
